﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

namespace AnOtherBlog.AJAX.Controls.MRPP
{
	public partial class Register : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			// SetUp Event Handlers
			CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);
			CreateUserWizard1.SendingMail += new MailMessageEventHandler(CreateUserWizard1_SendingMail);
			CreateUserWizard1.SendMailError += new SendMailErrorEventHandler(CreateUserWizard1_SendMailError);

			if (!IsPostBack)
			{

				CreateUserWizard1.CancelDestinationPageUrl = CreateUserWizard1.ContinueDestinationPageUrl = this.DestinationPageUrl;

				// TODO: revisar esta expresión regular (la copie de algún lado)
				CreateUserWizard1.EmailRegularExpression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

				CreateUserWizard1.HelpPageText = "";	// no help
				CreateUserWizard1.HeaderText = "";	// no header

				// TODO: agregar lo necesario para que funcione el envío de mail
				//CreateUserWizard1.MailDefinition.From = "";
				//CreateUserWizard1.MailDefinition.BodyFileName = "";
				//CreateUserWizard1.MailDefinition.Subject = Resources.MRPP.createUserWizardMailDefinitionSubject;

				CreateUserWizard1.PasswordRegularExpression = "";

				((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Focus();
			}
		}

		#region Fields and Properties

		/// <summary>
		/// Keeps Page Url for Cancel and Continue Destination
		/// </summary>
		private string destinationPageUrl;
		[Category("Links")]
		[DefaultValue("")]
		public string DestinationPageUrl
		{
			get
			{
				if (this.EnableViewState)
				{
					object tmp = ViewState["destinationPageUrl"];
					destinationPageUrl = (tmp == null) ? "" : (string)tmp;
				}
				return destinationPageUrl;
			}
			set
			{
				destinationPageUrl = value;
				if (this.EnableViewState)
				{
					ViewState["destinationPageUrl"] = destinationPageUrl;
				}
			}
		}

		#endregion



		#region Event Handlers
		
		/// <summary>
		/// Triggered after user is created
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
		{
			string userName = CreateUserWizard1.UserName;
			MembershipUser user = Membership.GetUser(userName);
			if (user == null)
			{
				throw new NotImplementedException("Something was wrong creating account for user: " + userName);
			}
			// Here we can do some like add user to specific roles or something else
		}
		/// <summary>
		/// Triggered before sending mail to new user
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.MailMessageEventArgs"/> instance containing the event data.</param>
		protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
		{
			// WARNING: SMTP SERVER RUNNING IS NEEDED TO TEST THIS
			// replace specific tags in .txt file with user or application settings
			// we need to write this body
			e.Message.Body = e.Message.Body.Replace(@"<%PasswordQuestion%>", CreateUserWizard1.Question);
			e.Message.Body = e.Message.Body.Replace(@"<%PasswordAnswer%>", CreateUserWizard1.Answer);
			// TODO: remove next line to enable sending mail
			e.Cancel = true;	// do not send mail using web.config settings
		}
		/// <summary>
		/// Triggered after an error in sending mail
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.SendMailErrorEventArgs"/> instance containing the event data.</param>
		void CreateUserWizard1_SendMailError(object sender, SendMailErrorEventArgs e)
		{
			// TODO: remove this in deployment environment
			e.Handled = true;	// I put this to prevent application aborting in my pc, I do not have smtp server
		}

	#endregion

	}
}